package com.worthtech.billportal.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.worthtech.billportal.bean.BillShop;
import com.worthtech.billportal.bean.BillShopbank;
import com.worthtech.billportal.bean.BillTradecode;
import com.worthtech.billportal.bean.WtPepJnls;
import com.worthtech.billportal.exception.SCException;
import com.worthtech.billportal.form.ShopForm;
import com.worthtech.billportal.service.ShopService;

public class ShopAction extends BaseAction {

	private ShopService shopService;

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		ShopForm form = (ShopForm) actionForm;
		if("tradelist".equals(form.getAction())){
			String sql = "";
			if(request.getParameter("addtrade")!=null){
				List shop = this.getShopService().getListByHQL("BillShop",
						0, 1000, "");
				request.setAttribute("shop", shop);
				return mapping.findForward("addtrade");
			}
			String first = request.getParameter("first");
			String up = request.getParameter("up");
			String down = request.getParameter("down");
			String last = request.getParameter("last");
			String page = request.getParameter("page");
			String shop2 = request.getParameter("shop");
			if(shop2 == null){
				shop2 = "all";
			}
			if(!shop2.equals("all")){
				sql += " where shopid =  "+shop2;
			}
			long start = System.currentTimeMillis();
			String tmp = this.getShopService().getCount("BillTradecode", sql);
			if (tmp == null) {
				tmp = "0";
			}
			int count = Integer.valueOf(tmp);
			int max = 0;
			if (count % 30 != 0) {
				max = count / 30 + 1;
			} else {
				max = count / 30;
			}
			if (request.getParameter("page") == null) {
				page = "1";
			}
			int page2 = Integer.valueOf(page) - 1;
			if (down != null) {
				page2 += 1;
			}
			if (up != null) {
				page2 -= 1;
			}
			if (first != null) {
				page2 = 0;
			}
			if (last != null) {
				page2 = max - 1;
			}
			if (page2 > max - 1) {
				page2 = max - 1;
			}
			if (page2 < 0) {
				page2 = 0;
			}
			if (count < 30) {
				page2 = 0;
			}
			List log = this.getShopService().getListByHQL("BillTradecode",
					page2 * 30, 30, sql);
			for(int i = 0;i<log.size();i++){
				BillTradecode b = (BillTradecode)log.get(i);
				String q = b.getShopid();
				b.setShopid(((BillShop)this.getShopService().getListByHQL("BillShop",page2 * 30, 30, "where id = "+q).get(0)).getName());
			}
			List shop = this.getShopService().getListByHQL("BillShop",
					0, 1000, "");
			
			request.setAttribute("page", page2 + 1 + "");
			request.setAttribute("totel", count + "");
			request.setAttribute("log", log);
			request.setAttribute("shop", shop);
			request.setAttribute("shopid", shop2);
			System.out.println("time= " + (System.currentTimeMillis() - start));
			return mapping.findForward("tradelist");
		}
		if ("list".equals(form.getAction())) {
			if(request.getParameter("addshop")!=null){
				return mapping.findForward("addshop");
			}
			String sql = "";
			String first = request.getParameter("first");
			String up = request.getParameter("up");
			String down = request.getParameter("down");
			String last = request.getParameter("last");
			String page = request.getParameter("page");
			long start = System.currentTimeMillis();
			String tmp = this.getShopService().getCount("BillShop", sql);
			if (tmp == null) {
				tmp = "0";
			}
			int count = Integer.valueOf(tmp);
			int max = 0;
			if (count % 30 != 0) {
				max = count / 30 + 1;
			} else {
				max = count / 30;
			}
			if (request.getParameter("page") == null) {
				page = "1";
			}
			int page2 = Integer.valueOf(page) - 1;
			if (down != null) {
				page2 += 1;
			}
			if (up != null) {
				page2 -= 1;
			}
			if (first != null) {
				page2 = 0;
			}
			if (last != null) {
				page2 = max - 1;
			}
			if (page2 > max - 1) {
				page2 = max - 1;
			}
			if (page2 < 0) {
				page2 = 0;
			}
			if (count < 30) {
				page2 = 0;
			}
			List log = this.getShopService().getListByHQL("BillShop",
					page2 * 30, 30, sql);
			request.setAttribute("page", page2 + 1 + "");
			request.setAttribute("totel", count + "");
			request.setAttribute("log", log);
			System.out.println("time= " + (System.currentTimeMillis() - start));
			return mapping.findForward("shoplist");
		}
		if ("addshop".equals(form.getAction())) {
				BillShop shop = new BillShop();
				shop.setFullName(form.getFullname());
				shop.setName(form.getName());
				shop.setPhone(form.getPhone());
				shop.setAddress(form.getAddress());
				shop.setZip(form.getZip());
				shop.setRemark(form.getRemark());
				try {
					this.getShopService().addShop(shop);
					return mapping.findForward("shoplist3");
				} catch (SCException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return mapping.findForward("error");
				}
		}
		if ("addtrade".equals(form.getAction())) {
			BillTradecode trade = new BillTradecode();
			trade.setShopid(request.getParameter("shopid"));
			trade.setType(form.getType());
			trade.setCharge(form.getCharge());
			trade.setChargeMode(request.getParameter("chargemode"));
			trade.setCode(form.getCode());
			trade.setRemark(form.getTraderemark());
			trade.setTypeMode(request.getParameter("typemode"));
			if(request.getParameter("typemode").equals("1")){
				trade.setChargeMode("0");
				trade.setCharge("0");
			}
			try {
				this.getShopService().addTrade(trade);
				return mapping.findForward("tradelist3");
			} catch (SCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return mapping.findForward("error");
			}
	}
		if("editbank".equals(form.getAction())){
			String id = request.getParameter("id");
			String shopid = request.getParameter("shopid");
			if(request.getParameter("edit")!=null){
				BillShopbank bank = new BillShopbank();
				bank.setAccbank(form.getAccbank());
				bank.setBankname(form.getBankname());
				bank.setId(Integer.valueOf(id));
				bank.setOpenbank(form.getOpenbank());
				bank.setRemark(form.getBankremark());
				bank.setShopid(Integer.valueOf(shopid));
				try {
					this.getShopService().updateBank(bank);
					request.setAttribute("id", shopid);
					return mapping.findForward("banklist3");
				} catch (SCException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return mapping.findForward("error");
				}
			}
			List log = this.getShopService().getListByHQL("BillShopbank", 0 * 30,
					30, " where id = " + id);
			BillShopbank shop = (BillShopbank) log.get(0);
			form.setBankname(shop.getBankname());
			form.setAccbank(shop.getAccbank());
			form.setBankremark(shop.getRemark());
			form.setOpenbank(shop.getOpenbank());
			request.setAttribute("shopid", shopid);
			request.setAttribute("id", id);
			return mapping.findForward("editbank");
		}
		if ("edit".equals(form.getAction())) {
			String id = request.getParameter("id");
			if (request.getParameter("edit") != null) {
				BillShop shop = new BillShop();
				shop.setFullName(form.getFullname());
				shop.setId(id);
				shop.setName(form.getName());
				shop.setPhone(form.getPhone());
				shop.setAddress(form.getAddress());
				shop.setZip(form.getZip());
				shop.setRemark(form.getRemark());
				try {
					this.getShopService().updateShop(shop);
					return mapping.findForward("shoplist3");
				} catch (SCException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return mapping.findForward("error");
				}
			}
			List log = this.getShopService().getListByHQL("BillShop", 0 * 30,
					30, " where id = " + id);
			BillShop shop = (BillShop) log.get(0);
			form.setFullname(shop.getFullName());
			form.setName(shop.getName());
			form.setPhone(shop.getPhone());
			form.setAddress(shop.getAddress());
			form.setZip(shop.getZip());
			form.setRemark(shop.getRemark());
			request.setAttribute("id", id);
			return mapping.findForward("editshop");
		}
		if("edittrade".equals(form.getAction())){
			String id = request.getParameter("id");
			if(request.getParameter("editcode")!=null){
				BillTradecode trade = new BillTradecode();
				trade.setId(Integer.valueOf(id));
				trade.setShopid(request.getParameter("shopid"));
				trade.setType(form.getType());
				trade.setCharge(form.getCharge());
				trade.setChargeMode(request.getParameter("chargemode"));
				trade.setCode(form.getCode());
				trade.setRemark(form.getTraderemark());
				trade.setTypeMode(request.getParameter("typemode"));
				if(request.getParameter("typemode").equals("1")){
					trade.setChargeMode("0");
					trade.setCharge("0");
				}
				try {
					this.getShopService().updateTrade(trade);
					return mapping.findForward("tradelist3");
				} catch (SCException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return mapping.findForward("error");
				}
			}
			List log = this.getShopService().getListByHQL("BillTradecode", 0 * 30,
					30, " where id = " + id);
			BillTradecode trade = (BillTradecode)log.get(0);
			form.setType(trade.getType());
			form.setTraderemark(trade.getRemark());
			form.setCharge(trade.getCharge());
			form.setCode(trade.getCode());
			List shop = this.getShopService().getListByHQL("BillShop",
					0, 1000, "");
			request.setAttribute("shopid", trade.getShopid());
			request.setAttribute("typemode", trade.getTypeMode());
			request.setAttribute("chargemode", trade.getChargeMode());
			request.setAttribute("shop", shop);
			request.setAttribute("id", id);
			return mapping.findForward("edittrade");
		}
		if ("del".equals(form.getAction())) {
			String id = request.getParameter("id");
			List log = this.getShopService().getListByHQL("BillShop", 0 * 30,
					30, " where id = " + id);
			BillShop shop = (BillShop) log.get(0);
			try {
				this.getShopService().delShop(shop);
				this.getShopService().delTable("BillShopbank", "where shopid = "+id);
				this.getShopService().delTable("BillTradecode", "where shopid = "+id);
				return mapping.findForward("shoplist3");
			} catch (SCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return mapping.findForward("error");
			}
		}
		if ("deltrade".equals(form.getAction())) {
			String id = request.getParameter("id");
			List log = this.getShopService().getListByHQL("BillTradecode", 0 * 30,
					30, " where id = " + id);
			BillTradecode shop = (BillTradecode) log.get(0);
			try {
				this.getShopService().delTrade(shop);
				return mapping.findForward("tradelist3");
			} catch (SCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return mapping.findForward("error");
			}
		}
		if("delbank".equals(form.getAction())){
			String id = request.getParameter("id");
			String shopid = request.getParameter("shopid");
			List log = this.getShopService().getListByHQL("BillShopbank", 0 * 30,
					30, " where id = " + id);
			BillShopbank shop = (BillShopbank) log.get(0);
			try {
				this.getShopService().delBank(shop);
				request.setAttribute("id", shopid);
				System.out.println("--------------------------id="+shopid);
				return mapping.findForward("banklist3");
			} catch (SCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return mapping.findForward("error");
			}
		}
		if ("addbank".equals(form.getAction())) {
				String id = request.getParameter("id");
				BillShopbank bank = new BillShopbank();
				bank.setShopid(Integer.valueOf(id));
				bank.setAccbank(form.getAccbank());
				bank.setBankname(form.getBankname());
				bank.setOpenbank(form.getOpenbank());
				bank.setRemark(form.getBankremark());
				try {
					this.getShopService().addBank(bank);
					request.setAttribute("id", id);
					return mapping.findForward("banklist3");
				} catch (SCException e) {
					e.printStackTrace();
					return mapping.findForward("error");
				}
			
		}
		if ("banklist".equals(form.getAction())) {
			String id = request.getParameter("id");
			if (request.getAttribute("id")!= null) {
				id = (String) request.getAttribute("id");
			}
			if(request.getParameter("addbank")!=null){
				request.setAttribute("id", id);
				return mapping.findForward("addbank");
			}
			String sql = " where shopid = " + id;
			String first = request.getParameter("first");
			String up = request.getParameter("up");
			String down = request.getParameter("down");
			String last = request.getParameter("last");
			String page = request.getParameter("page");
			long start = System.currentTimeMillis();
			String tmp = this.getShopService().getCount("BillShopbank", sql);
			if (tmp == null) {
				tmp = "0";
			}
			int count = Integer.valueOf(tmp);
			int max = 0;
			if (count % 30 != 0) {
				max = count / 30 + 1;
			} else {
				max = count / 30;
			}
			if (request.getParameter("page") == null) {
				page = "1";
			}
			int page2 = Integer.valueOf(page) - 1;
			if (down != null) {
				page2 += 1;
			}
			if (up != null) {
				page2 -= 1;
			}
			if (first != null) {
				page2 = 0;
			}
			if (last != null) {
				page2 = max - 1;
			}
			if (page2 > max - 1) {
				page2 = max - 1;
			}
			if (page2 < 0) {
				page2 = 0;
			}
			if (count < 30) {
				page2 = 0;
			}
			List log = this.getShopService().getListByHQL("BillShopbank",
					page2 * 30, 30, sql);
			request.setAttribute("page", page2 + 1 + "");
			request.setAttribute("totel", count + "");
			request.setAttribute("id", id);
			request.setAttribute("log", log);
			System.out.println("time= " + (System.currentTimeMillis() - start));
			return mapping.findForward("banklist");
		}
		return null;
	}

}
